function [Yhat] = ecogPredictRegression(X,W, param)
%[Yhat] = ecogPredictRegression(X,W, param)
%
% Purpose: Apply Multiple Input Multiple Output (MIMO) Wiener regression 
% to predict Yhat from X accoring to Yhat=X*W, i.e. predicts Y 
% (the movement or stimulus feature)from X (the ecog data).
%
% Yhat is predicted from multiple shifted version of X because W extends in
% time and establishes a certain temporal relation between X and Y that can 
% be interpreted in terms of causality (see below). Heres X causal on Y means
% that effects in X have an influence on what happens later in time in Y. 
% 
% INPUT:
% X:        Neuronal data prepared with ecogPrepareRegression
% W:        The matrix W as returned by ecogWienerMIMORegressionTrain
%           The first entry might be an offset and will be involved in the reconstruction(CR 4.6.2010).

% param: parameter optionally determined in ecogTrainRegression
% OUTPUT:
% Yhat:     The predicted Y-values.  
%           Due to the filter length some Yhat-values at the end will be
%           set to zero. Moreover, with negative offsets some Yhat-values at
%           the begin will be zero. This is because the Wiener regression 
%           working backwards in time cannot predit all Yhats at the begin. 
%           NEED A WORKAROUND FOR THIS: MAYBE PREPENDING X with zeros
%
% Requirements: 

% 091129 JR wrote it based on Brian's code
% 111111 CR: create sub functions ecogPrepareRegression,
% ecogTrainRegression

%% Input check
if nargin <3,
    param.normalize = struct;
end

%% zscore
if isfield(param.normalize,'meanX'),
    X = (X-ones(size(X,1),1)*param.normalize.meanX)./(ones(size(X,1),1)*param.normalize.stdX);
end
%% Now the prediction: 
%Yhat=X*W;
if isfield(param,'svr'),
    tst = test(param.svr,data(X));
    Yhat = tst.X;
else
    Yhat=[ones(size(X,1),1),X]*W;
end

if isfield(param.normalize,'meanY'),
    Yhat = (Yhat.*(ones(size(Yhat,1),1)*param.normalize.stdY))+ones(size(Yhat,1),1)*param.normalize.meanY;
end
